import { Form, Input,Select } from 'antd';
import React,{useEffect,useState} from 'react';
import { request } from 'umi';

interface PropsType {
  name?:any,
  formItem:any,
  restField?:any
}
const App: React.FC<PropsType> =(props) => {
  // useImperativeHandle(ref,()=>({}))
  const [selectData, setselectData] = useState([]);
  useEffect(()=>{
    async function getData(){
      if(props.formItem?.type==='selectForm'){
        let msg = await request(props.formItem.getApi);
        if(msg.flag){
          props.formItem.value=msg.data
         setselectData([...selectData,...msg.data])
        }
       }
    }
    getData()
  },[])
  return (
          <Form.Item
            {...props.restField||{}}
            label={props.formItem.plabel}
            name={props.name?props.name:props.formItem.pname}
            rules={[{ required: true, message: `请输入${props.formItem.plabel}`} ]}
      >
        {props.formItem?.type==='selectForm' && <Select
      // defaultValue={[props.formItem.defaultValue||'']}
      style={props.formItem.style||{ width: 300 }}
      options={selectData}
      mode={props.formItem.mode}
      placeholder={`请输入${props.formItem.plabel}`}
      fieldNames={props.formItem.fieldNames||{ label: 'label', value: 'value' }}
    />}
            {!props.formItem?.type &&<Input style={props.formItem.style} placeholder={`请输入${props.formItem.plabel}`} />}
          </Form.Item>

  );
}

export default App;
